package com.atguigu.service.impl;

import com.atguigu.dao.SubtaskDao;
import com.atguigu.dao.impl.SubtaskDaoImpl;
import com.atguigu.pojo.Subtask;
import com.atguigu.pojo.Page;
import com.atguigu.service.SubtaskService;

import java.util.List;

public class SubtaskServiceImpl implements SubtaskService {
    SubtaskDao subtaskDao=new SubtaskDaoImpl();
    @Override
    public void addSubtask(Subtask subtask) {
        subtaskDao.addSubtask(subtask);
    }

    @Override
    public void updateSubtask(Subtask subtask) {
        subtaskDao.updateSubtask(subtask);
    }

    @Override
    public void setSubtaskStatusByMainTaskId(String mainTaskId,String status){
        subtaskDao.setSubtaskStatusByMainTaskId(mainTaskId,status);
    }

    @Override
    public void deleteSubtaskById(String id) {
        subtaskDao.deleteSubtaskById(id);
    }

    @Override
    public void deleteSubtaskByMainTaskId(String mainTaskId) {
        subtaskDao.deleteSubtaskByMainTaskId(mainTaskId);
    }

    @Override
    public Subtask querySubtaskById(String id) {
        Subtask subtask=subtaskDao.querySubtaskById(id);
        return subtask;
    }
    @Override
    public Integer querySubtasksByStatusNotTrue(){
        //System.out.println(subtaskDao.querySubtasksByStatusNotTrue());
        Integer number=subtaskDao.querySubtasksByStatusNotTrue();
        return number;
    }

    @Override
    public List<Subtask> querySubtasksByServerId(String serverId) {
        List<Subtask> subtasks=subtaskDao.querySubtasksByServerId(serverId);
        return subtasks;
    }
    @Override
    public List<Subtask> queryNeedDoSubtasksByMainTaskId(String mainTaskId) {
        List<Subtask> subtasks=subtaskDao.queryNeedDoSubtasksByMainTaskId(mainTaskId);
        return subtasks;
    }
    @Override
    public List<Subtask> querySubtasksByMainTaskId(String mainTaskId){
        List<Subtask> subtasks=subtaskDao.querySubtasksByMainTaskId(mainTaskId);
        return subtasks;
    }
    @Override
    public List<Subtask> queryNeedDoSubtasksByServerId(String serverId) {
        List<Subtask> subtasks=subtaskDao.queryNeedDoSubtasksByServerId(serverId);
        return subtasks;
    }

    @Override
    public List<Subtask> querySubtasksByCommand(String subtaskCommand) {
        List<Subtask> subtasks=subtaskDao.querySubtasksByCommand(subtaskCommand);
        return subtasks;
    }
    @Override
    public List<Subtask> querySubtasksByServerIdAndSubtaskCommand(String serverId, String subtaskCommand) {
        List<Subtask> subtasks=subtaskDao.querySubtasksByServerIdAndSubtaskCommand(serverId,subtaskCommand);
        return subtasks;
    }
    @Override
    public List<Subtask> getSpeciallyAppointedTargetNodeMainTaskResult(String targetNodeId,String mainTaskId){
        List<Subtask> subtasks=subtaskDao.getSpeciallyAppointedTargetNodeMainTaskResult(targetNodeId,mainTaskId);
        return subtasks;
    }
    @Override
    public List<Subtask> querySubtasks() {
        List<Subtask> subtasks=subtaskDao.querySubtasks();
        return subtasks;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<Subtask> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=subtaskDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<Subtask> items=subtaskDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
